package com.etogc.groupchat.group.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.etogc.groupchat.R;
import com.etogc.groupchat.base.BaseActivity;
import com.etogc.groupchat.group.adpt.SearchAdapter;
import com.etogc.groupchat.group.biz.GroupHttpBiz2;
import com.etogc.http.ReqDataHandler;
import com.etogc.http.ReqErrorHandler;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.util.YcToastHelper;
import com.etogc.view.LoadDialog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 搜索页面
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {

    EditText mSearch;

    ImageView mClear;


    RecyclerView mRecyclerView;

    private List<HashMap<String, Object>> mSearchList = new ArrayList<>();

    private SearchAdapter mSearchAdapter;

    private TextView addCare;

    private String mUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        findViewById(R.id.search).setOnClickListener(this);
        findViewById(R.id.icon_back).setOnClickListener(this);
        findViewById(R.id.iv_clear).setOnClickListener(this);
        mRecyclerView = findViewById(R.id.recycler_view);
        mSearch = findViewById(R.id.et_search);
        mClear = findViewById(R.id.iv_clear);
        initUI();
    }

    /**
     * 初始化UI数据
     */
    private void initUI() {
        //设置适配器
        mSearchAdapter = new SearchAdapter(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mSearchAdapter);

//        //监听输入框
//        mSearch.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (s.length() > 0) {
//                    mClear.setVisibility(View.VISIBLE);
//                } else {
//                    mClear.setVisibility(View.GONE);
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });

    }

    /**
     * 设置点击事件
     */
    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            //返回上一级页面
            case R.id.icon_back:
                finish();
                break;

            //返回上一级页面
            case R.id.iv_clear:
                mSearch.setText("");
                mSearchList.clear();
                mSearchAdapter.notifyDataSetChanged();
                break;

            //搜索
            case R.id.search:
                String groupName = mSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(groupName)) {
                    requestSearchData(groupName);
                } else {
                    YcToastHelper.getInstance()._toast("请输入ID搜索");
                }
                hiddenInputMethod(this);
                break;
        }
    }

    public static void hiddenInputMethod(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    /**
     * 搜索数据
     */
    public void requestSearchData(String groupName) {
        GroupHttpBiz2.getSearchList(this, groupName, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(SearchActivity.this);
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    Map<String, Object> map = ReqDataHandler.getMapWithEntityNoGson(response);
                    List<HashMap<String, String>> list = (List<HashMap<String, String>>) map.get("groupList");
                    if (list != null && list.size() > 0) {
                        mSearchAdapter.setData(list);
                    } else {
                        YcToastHelper.getInstance()._toast("暂无数据");
                    }
                    YcToastHelper.getInstance()._toast("搜索成功");
                }
            }
        });

    }
}
